//
//  signup.m
//  testvideo
//
//  Created by fcbayern on 2021/1/4.
//  Copyright © 2021 fcbayern. All rights reserved.
//

#import "signup.h"
#import "Masonry.h"
#import "AFNetworking.h"
#import <objc/runtime.h>
@interface signup ()

@end

@implementation signup

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.view.backgroundColor = [UIColor blackColor];
    _name = [[UITextField alloc]init];
    _email =[[UITextField alloc]init];
    _password = [[UITextField alloc]init];
    _password.secureTextEntry = YES;
    _passwordconfirm =[[UITextField alloc]init];
    _passwordconfirm.secureTextEntry = YES;
    _signup = [[UIButton alloc]init];
    _cancel  =[[UIButton alloc]init];
    _name.layer.borderWidth = 1.0f;
    _name.layer.cornerRadius = 15;
    _password.layer.borderWidth = 1.0f;
     _password.layer.cornerRadius = 15;
    _email.layer.borderWidth = 1.0f;
    _email.layer.cornerRadius = 15;
    _passwordconfirm.layer.borderWidth = 1.0f;
    _passwordconfirm.layer.cornerRadius = 15;
    _cancel.layer.borderWidth = 1.0f;
    _cancel.layer.cornerRadius = 15;
     _signup.layer.borderWidth = 1.0f;
    _signup.layer.cornerRadius = 15;
    _name.placeholder = @"请输入用户名";
    Ivar ivar1 =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel1 = object_getIvar(_name, ivar1);
    placeholderLabel1.textColor = [UIColor whiteColor];
    
    
    _password.placeholder = @"请输入密码";
    _passwordconfirm.placeholder = @"请再次输入相同的密码";
    Ivar ivar3 =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel3 = object_getIvar(_passwordconfirm, ivar3);
    placeholderLabel3.textColor = [UIColor whiteColor];
    _email.placeholder = @"请输入注册使用的邮箱";
    Ivar ivar2 =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel2 = object_getIvar(_email, ivar2);
    placeholderLabel2.textColor = [UIColor whiteColor];
    
    Ivar ivar4 =  class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *placeholderLabel4 = object_getIvar(_password, ivar4);
    placeholderLabel4.textColor = [UIColor whiteColor];
    _name.textColor = [UIColor whiteColor];
    _email.textColor = [UIColor whiteColor];
    _password.textColor = [UIColor whiteColor];
    _passwordconfirm.textColor = [UIColor whiteColor];
    
    [_signup setTitle:@"注册" forState:UIControlStateNormal];
    [_cancel setTitle:@"取消" forState:UIControlStateNormal];
    _signup.backgroundColor = [UIColor colorWithRed:228/255.0 green:40/255.0 blue:74/255.0 alpha:1];
    _cancel.backgroundColor =[UIColor colorWithRed:228/255.0 green:40/255.0 blue:74/255.0 alpha:1];
    
    
    [_signup setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_signup setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    [_cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_cancel setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    
    [self.view addSubview:_name];
    [self.view addSubview:_password];
    [self.view addSubview:_passwordconfirm];
    [self.view addSubview:_cancel];
    [self.view addSubview:_signup];
    [self.view addSubview:_email];
    
    [_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.08);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.7);
        make.top.equalTo(self.view.mas_top).with.offset(self.view.frame.size.height*0.12);
        
    }];
    [_email mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.08);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.7);
        make.top.equalTo(_name.mas_top).with.offset(self.view.frame.size.height*0.12);
        
    }];
    [_password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.08);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.7);
        make.top.equalTo(_email.mas_top).with.offset(self.view.frame.size.height*0.12);
        
    }];
    [_passwordconfirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.08);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.7);
        make.top.equalTo(_password.mas_top).with.offset(self.view.frame.size.height*0.12);
        
    }];
    [_cancel mas_makeConstraints:^(MASConstraintMaker *make) {
           //make.centerX.equalTo(self.view.mas_centerX);
        make.left.equalTo(self.view.mas_left).with.offset(self.view.frame.size.width*0.1);
           make.height.equalTo(self.view.mas_height).multipliedBy(0.1);
           make.width.equalTo(self.view.mas_width).multipliedBy(0.3);
           make.top.equalTo(_passwordconfirm.mas_top).with.offset(self.view.frame.size.height*0.2);
           
       }];
    [_signup mas_makeConstraints:^(MASConstraintMaker *make) {
        //make.centerX.equalTo(self.view.mas_centerX);
     make.right.equalTo(self.view.mas_right).with.offset(-self.view.frame.size.width*0.1);
        make.height.equalTo(self.view.mas_height).multipliedBy(0.1);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.3);
        make.top.equalTo(_passwordconfirm.mas_top).with.offset(self.view.frame.size.height*0.2);
        
    }];
    [_cancel addTarget:self action:@selector(cancelactivity:) forControlEvents:UIControlEventTouchUpInside];
    [_signup addTarget:self action:@selector(signupactivity:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)cancelactivity:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)signupactivity:(id)sender{
    NSString * Username = _name.text;
    NSString * Password = _password.text;
    NSString * Email = _email.text;
    NSString * confirm = _passwordconfirm.text;
    if(Username.length == 0 || Password.length == 0 || Email.length == 0){
        UIAlertController * alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"信息不能为空" preferredStyle:UIAlertControllerStyleAlert];
               [self presentViewController:alert1 animated:YES completion:nil];
               dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                   [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
               });
    }
    else if([Password isEqualToString:confirm] == 0){
        UIAlertController * alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次输入的密码不同" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert1 animated:YES completion:nil];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
        });
    }
    
    NSDictionary * parameters = @{
        @"name":Username,
         @"email":Email,
        @"password":Password
    };
    NSString * url = @"http://106.52.91.37:8081/api/user/register";
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager POST:url parameters:parameters headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary * returnvalue = (NSDictionary *)responseObject;
        NSString * State = returnvalue[@"Type"];
        if([State isEqualToString:@"success"]){
            UIAlertController * alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
                   [self presentViewController:alert1 animated:YES completion:nil];
                   dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                       [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
                   });
            [self.navigationController popViewControllerAnimated:NO];
        }
        else if([State isEqualToString:@"email exists"]){
            UIAlertController * alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"邮箱已被注册" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert1 animated:YES completion:nil];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
            });
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
